package com.binary.markdownrecord.config.snowflake;


import com.binary.markdownrecord.util.SpringUtil;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IncrementGenerator;

import java.io.Serializable;


public class SnowflakeIdGenerator extends IncrementGenerator {
    private static SnowflakeIdWorker snowflakeIdWorker;


    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        if (snowflakeIdWorker == null) {
            snowflakeIdWorker = SpringUtil.getBean(SnowflakeIdWorker.class);
        }
        Object id = snowflakeIdWorker.nextId();
        return (Serializable) id;
    }
}
